package com.ssy.lingxi.platform.manage.serviceimpl.paas;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.manage.api.model.dto.currency.CurrencyPAASDTO;
import com.ssy.lingxi.platform.manage.entity.base.Currency;
import com.ssy.lingxi.platform.manage.repository.CurrencyRepository;
import com.ssy.lingxi.platform.manage.service.paas.ICurrencyPAASService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @menu 币制 - （系统初始化）PAAS接口 - 业务实现层
 * @author fhj
 * @version 2.0.0
 * @date 2020/7/20
 */
@Service
public class CurrencyPAASServiceImpl implements ICurrencyPAASService {

    @Resource
    private CurrencyRepository currencyRepository;

    @Override
    @Transactional
    public Wrapper<Void> add(CurrencyPAASDTO dto) {
        Currency currency = new Currency();
        BeanUtils.copyProperties(dto, currency);
        currencyRepository.saveAndFlush(currency);
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> update(CurrencyPAASDTO dto) {
        Currency currency = currencyRepository.findById(dto.getId()).orElse(null);
        if(currency == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        BeanUtils.copyProperties(dto, currency);
        currencyRepository.saveAndFlush(currency);
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> delete(CurrencyPAASDTO dto) {
        Currency currency = currencyRepository.findById(dto.getId()).orElse(null);
        if(currency == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        currencyRepository.delete(currency);
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> state(CurrencyPAASDTO dto) {
        Currency currency = currencyRepository.findById(dto.getId()).orElse(null);
        if(currency == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        currency.setState(dto.getState());
        currencyRepository.saveAndFlush(currency);
        return Wrapper.success();
    }
}
